<?php
/**
* Benchmark class file.
*
* @author Federico Ramirez <fedekiller@gmail.com>
* @link http://code.google.com/p/akaikiwi/
* @copyright Copyright &copy; 2008-2011 Federico Ramirez
* @license http://www.opensource.org/licenses/mit-license.php MIT Licence
*/

/*
* This class implements benchmarking
*/
class Benchmark {
	private $marks;

	function __construct() {
		$this->marks = array();
	}

	/**
	 * Adds a new mark with the given name
	 * @param String $name
	 * @throws AkaikiwiException
	 */
	public function add_mark($name) {
		if(!is_string($name)) {
			throw new AkaikiwiException('Invalid argument for add_mark, it must be a string');
		}

		$this->marks[$name] = microtime(TRUE);
	}

	/**
	 * Gets the requested mark
	 * @param String $name
	 * @throws AkaikiwiException
	 * @return float The requested mark
	 */
	public function get_mark($name) {
		if(!is_string($name)) {
			throw new AkaikiwiException('Invalid argument for add_mark, it must be a string');
		}

		return $this->marks[$name];
	}

	/**
	 * Get the marks array
	 */
	public function get_marks() {
		return $this->marks;
	}

	/**
	 * Get the seconds elapsed between those two marks
	 * @param String $mark1
	 * @param String $mark2
	 * @throws AkaikiwiException
	 * @return float The time elapsed between those two marks in seconds
	 */
	public function get_time($mark1, $mark2) {
		if(!(array_key_exists($mark1, $this->marks) && array_key_exists($mark2, $this->marks))) {
			throw new AkaikiwiException('Invalid marks names.');
		}
		return round(abs($this->marks[$mark1] - $this->marks[$mark2]), 3);
	}
}
